package com.zqxx.examine.portal.controller.action;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zqxx.examine.model.exam.Message;
import com.zqxx.examine.model.user.User;
import com.zqxx.examine.portal.security.StandardPasswordEncoderForSha1;
import com.zqxx.examine.portal.security.UserInfo;
import com.zqxx.examine.portal.service.UserService;

@Controller
public class UserAction
{
  @Autowired
  private UserService userService;
  
  @RequestMapping(value={"/add-user"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
  @ResponseBody
  public Message addUser(@RequestBody User user)
  {
    user.setCreateTime(new Date());
    
    String password = user.getPassword() + "{" + user.getUserName().toLowerCase() + "}";
    PasswordEncoder passwordEncoder = new StandardPasswordEncoderForSha1();
    String resultPassword = passwordEncoder.encode(password);
    user.setPassword(resultPassword);
    user.setEnabled(true);
    user.setCreateBy(-1);
    user.setUserName(user.getUserName().toLowerCase());
    Message message = new Message();
    try
    {
      this.userService.addUser(user, "ROLE_STUDENT", 0, this.userService.getRoleMap());
    }
    catch (Exception e)
    {
      if (e.getMessage().contains(user.getUserName()))
      {
        message.setResult("duplicate-username");
        message.setMessageInfo("重复的用户名");
      }
      else if (e.getMessage().contains(user.getNationalId()))
      {
        message.setResult("duplicate-national-id");
        message.setMessageInfo("重复的身份证");
      }
      else if (e.getMessage().contains(user.getEmail()))
      {
        message.setResult("duplicate-email");
        message.setMessageInfo("重复的邮箱");
      }
      else if (e.getMessage().contains(user.getPhoneNum()))
      {
        message.setResult("duplicate-phone");
        message.setMessageInfo("重复的电话");
      }
      else
      {
        message.setResult(e.getCause().getMessage());
        e.printStackTrace();
      }
    }
    return message;
  }
  
  @RequestMapping(value={"/student/update-user"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
  @ResponseBody
  public Message updateUser(@RequestBody User user)
  {
    UserInfo userInfo = (UserInfo)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    
    String password = user.getPassword() + "{" + user.getUserName() + "}";
    PasswordEncoder passwordEncoder = new StandardPasswordEncoderForSha1();
    String resultPassword = "";
    if (user.getPassword() != null) {
      resultPassword = "".equals(user.getPassword().trim()) ? "" : passwordEncoder.encode(password);
    }
    user.setPassword(resultPassword);
    user.setEnabled(true);
    Message message = new Message();
    try
    {
      this.userService.updateUser(user, null);
      userInfo.setTrueName(user.getTrueName());
      userInfo.setEmail(user.getEmail());
      userInfo.setNationalId(user.getNationalId());
      userInfo.setPhoneNum(user.getPhoneNum());
    }
    catch (Exception e)
    {
      if (e.getMessage().contains(user.getUserName()))
      {
        message.setResult("duplicate-username");
        message.setMessageInfo("重复的用户名");
      }
      else if (e.getMessage().contains(user.getNationalId()))
      {
        message.setResult("duplicate-national-id");
        message.setMessageInfo("重复的身份证");
      }
      else if (e.getMessage().contains(user.getEmail()))
      {
        message.setResult("duplicate-email");
        message.setMessageInfo("重复的邮箱");
      }
      else if (e.getMessage().contains(user.getPhoneNum()))
      {
        message.setResult("duplicate-phone");
        message.setMessageInfo("重复的电话");
      }
      else
      {
        message.setResult(e.getCause().getMessage());
        e.printStackTrace();
      }
    }
    return message;
  }
  
  @RequestMapping(value={"/student/change-pwd"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
  @ResponseBody
  public Message changePassword(@RequestBody User user)
  {
    Message message = new Message();
    
    UserInfo userInfo = (UserInfo)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    try
    {
      String password = user.getPassword() + "{" + userInfo.getUsername() + "}";
      PasswordEncoder passwordEncoder = new StandardPasswordEncoderForSha1();
      String resultPassword = passwordEncoder.encode(password);
      user.setPassword(resultPassword);
      user.setUserName(userInfo.getUsername());
      this.userService.updateUserPwd(user, null);
    }
    catch (Exception e)
    {
      e.printStackTrace();
      message.setResult(e.getClass().getName());
    }
    return message;
  }
}
